package main

import (
	"gpt-api/framework/auther/jwtauth"
	"gpt-api/framework/cache"
	"gpt-api/framework/cfg"
	"gpt-api/framework/db"
	"gpt-api/framework/ginplus"
	"gpt-api/framework/interceptors"
	"gpt-api/framework/log"
	"gpt-api/framework/log/writer"
	"gpt-api/handler"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func NewRouter() (*gin.Engine, error) {
	// create roll.log at log directory.
	// each log file limits 50MB.
	// max 10 files.
	wt, err := writer.NewWriter(viper.GetString("log.path"), 50*1024*1024, viper.GetInt("log.maxfiles"))
	if err != nil {
		return nil, err
	}
	log.SetOutput(wt)
	// log.SetOutput(os.Stdout)
	dbwt, err := writer.NewWriter(viper.GetString("log.dbpath"), 50*1024*1024, viper.GetInt("log.maxfiles"))
	if err != nil {
		return nil, err
	}
	db.SetLogOutput(dbwt)
	// db.SetLogOutput(os.Stdout)
	if viper.GetString("auth.mode") != "debug" {
		gin.SetMode(gin.ReleaseMode)
	}

	//connSingleInstance := design.GetSingleInstance()

	// 初始化资源
	// db redis
	redisCli := cfg.NewRedis("redis")
	store := cache.NewCache(redisCli, &cache.Config{})

	auth, err := jwtauth.InitJWTAuth(store)
	if err != nil {
		return nil, err
	}
	router := gin.New()
	router.Use(interceptors.Logger(interceptors.IgnorePathLogSkipper(
		"/healthz",
	)))
	router.Use(interceptors.Recovery())
	router.Use(interceptors.RateLimit())
	router.Use(interceptors.CORS())
	router.Use(interceptors.Cookie())
	router.Use(interceptors.Preflight(auth))
	//不需要鉴权的
	skip := interceptors.AllowPathPrefixSkipper(
		"/api/v1/common/wxlogin",
		"/api/v1/common/wxpayasyn",
		"/api/v1/topupways/",
		"/api/v1/test/",
		"/swagger/",
	)
	router.Use(interceptors.UserAuth(auth, skip))

	hd := handler.NewHandler(redisCli, auth)

	v1 := router.Group("/api/v1")
	{
		// 用户相关
		userGroup := v1.Group("/users")
		{
			userGroup.GET("/info", ginplus.Wrapper(hd.GetUser))
			userGroup.PUT("/info", ginplus.Wrapper(hd.UpdateUser))
			userGroup.POST("image/upload", ginplus.Wrapper(hd.UploadImage))
			userGroup.GET("/invite/code", ginplus.Wrapper(hd.GetUserInviteCode))
			userGroup.GET("/invite/record", ginplus.Wrapper(hd.GetUserInviteRecord))
		}

		// 公共接口
		commonGroup := v1.Group("/common")
		{
			commonGroup.GET("/wxlogin", ginplus.Wrapper(hd.WeiXinLogin))
			commonGroup.GET("/province", ginplus.Wrapper(hd.Province))
			commonGroup.GET("/city", ginplus.Wrapper(hd.City))
			commonGroup.GET("/area", ginplus.Wrapper(hd.Area))
			commonGroup.POST("/wxpay", ginplus.Wrapper(hd.WechatPay))
			commonGroup.GET("/wxpay", ginplus.Wrapper(hd.WechatPaySync))
			commonGroup.POST("/wxpayasyn", hd.WechatPayAsyn)
		}

		// 充值方式相关
		topupwaysGroup := v1.Group("/topupways")
		{
			topupwaysGroup.GET("/list", ginplus.Wrapper(hd.ListTopupWays))
		}

		// 提现账户相关
		cashAccountGroup := v1.Group("/cashaccount")
		{
			cashAccountGroup.GET("/list", ginplus.Wrapper(hd.ListCashAccount))
			cashAccountGroup.POST("/add", ginplus.Wrapper(hd.AddCashAccount))
		}

		// 提现账户相关
		transferGroup := v1.Group("/transfer")
		{
			transferGroup.GET("/list", ginplus.Wrapper(hd.ListTransfer))
			transferGroup.POST("/add", ginplus.Wrapper(hd.ApplyTransfer))
		}

		// 通知相关
		notifyGroup := v1.Group("/notifications")
		{
			notifyGroup.GET("/list", ginplus.Wrapper(hd.ListNotifications))
			notifyGroup.GET("/info", ginplus.Wrapper(hd.GetNotification))
			notifyGroup.DELETE("/delete", ginplus.Wrapper(hd.DeleteNotification))
			notifyGroup.PUT("/markread", ginplus.Wrapper(hd.MarkNotificationRead))
			notifyGroup.PUT("/markallread", ginplus.Wrapper(hd.MarkAllNotificationsRead))
		}

		// 家族成员相关
		memberGroup := v1.Group("/members")
		{
			memberGroup.GET("/list", ginplus.Wrapper(hd.ListMembers))
			memberGroup.GET("/list/all", ginplus.Wrapper(hd.ListMembersAll))
			memberGroup.GET("/list/child", ginplus.Wrapper(hd.ListMembersChild))
			memberGroup.POST("/add", ginplus.Wrapper(hd.AddMember))
			memberGroup.PUT("/update", ginplus.Wrapper(hd.UpdateMember))
			memberGroup.DELETE("/delete", ginplus.Wrapper(hd.DeleteMember))
			memberGroup.GET("/info", ginplus.Wrapper(hd.GetMember))
			memberGroup.GET("/images", ginplus.Wrapper(hd.GetMemberImages))
			memberGroup.POST("/images", ginplus.Wrapper(hd.AddMemberImage))
			memberGroup.DELETE("/images", ginplus.Wrapper(hd.DeleteMemberImage))
		}

		// 事件相关
		eventGroup := v1.Group("/events")
		{
			eventGroup.GET("/list", ginplus.Wrapper(hd.ListEvents))
			eventGroup.POST("/add", ginplus.Wrapper(hd.AddEvent))
			eventGroup.PUT("/info", ginplus.Wrapper(hd.UpdateEvent))
			eventGroup.DELETE("/delete", ginplus.Wrapper(hd.DeleteEvent))
			eventGroup.GET("/info", ginplus.Wrapper(hd.GetEvent))
		}

		// 事件类型相关
		eventTypeGroup := v1.Group("/eventtype")
		{
			eventTypeGroup.GET("/list", ginplus.Wrapper(hd.ListEventTypes))
			eventTypeGroup.POST("/add", ginplus.Wrapper(hd.AddEventType))
			eventTypeGroup.PUT("/info", ginplus.Wrapper(hd.UpdateEventType))
			eventTypeGroup.DELETE("/delete", ginplus.Wrapper(hd.DeleteEventType))
			eventTypeGroup.GET("/info", ginplus.Wrapper(hd.GetEventType))
		}

		// 亲情相关
		relationsGroup := v1.Group("/relations")
		{
			relationsGroup.GET("/list", ginplus.Wrapper(hd.ListRelations))
			relationsGroup.GET("/children", ginplus.Wrapper(hd.ListRelationChildren))
		}

		// test
		testGroup := v1.Group("/test")
		{
			testGroup.GET("/get", ginplus.Wrapper(hd.GetTest))
			testGroup.POST("/post", ginplus.Wrapper(hd.PostTest))
		}

		// 图片处理
		imgGroup := v1.Group("/img")
		{
			imgGroup.POST("/process", ginplus.Wrapper(hd.ImgProcess))
			imgGroup.POST("/video", ginplus.Wrapper(hd.ImgToVideo))
			imgGroup.GET("/save", ginplus.Wrapper(hd.SaveImg))
			imgGroup.DELETE("/delete", ginplus.Wrapper(hd.DeleteImg))
		}

		// exchange code
		exchangeGroup := v1.Group("/exchange")
		{
			exchangeGroup.GET("/ex", ginplus.Wrapper(hd.ExchangeCode))
		}

	}

	// //样例列表
	// router.POST("/api/qlist", ginplus.Wrapper(hd.ListResult))
	// //判断用户状态
	// router.POST("/api/v1/isvip", ginplus.Wrapper(hd.VipResult))

	// router.POST("/api/adlog", ginplus.Wrapper(hd.AddLogs))

	// //添加用户注册接口
	// router.POST("/v1/api/register", ginplus.Wrapper(hd.AddUserResult))
	// //照片增强
	// router.POST("/v1/api/photo/enhancement", ginplus.Wrapper(hd.AddUserResult))
	// //照片上色
	// router.POST("/v1/api/photo/Color", ginplus.Wrapper(hd.AddUserResult))
	// //照片动画
	// router.POST("/v1/api/photo/animation", ginplus.Wrapper(hd.AddUserResult))
	// //事件添加
	// router.POST("/v1/api/event/add", ginplus.Wrapper(hd.AddEventResult))
	// //事件编辑
	// router.POST("/v1/api/event/edit", ginplus.Wrapper(hd.UpdateEventResult))
	// //事件列表
	// router.POST("/v1/api/event/list", ginplus.Wrapper(hd.GetEventsResult))
	// //判断用户状态
	// router.POST("/v1/api/user/isvip", ginplus.Wrapper(hd.VipResult))
	// //下单接口
	// router.POST("/v1/api/order/place", ginplus.Wrapper(hd.ChatGptResult)) //下单购买
	// // 充值商品信息列表
	// router.POST("/api/products", ginplus.Wrapper(hd.CoinsResult))
	// // 应用列表
	// router.POST("/api/apply", ginplus.Wrapper(hd.ApplyResult))
	// // 成员添加
	// router.POST("/api/add/member", ginplus.Wrapper(hd.AddMembersResult))
	// //成员关系添加

	// //支付回调
	// router.POST("/api/coin/charger", ginplus.Wrapper(hd.PayStatusNotify))
	// router.POST("/api/pay/notify", ginplus.Wrapper(hd.CoinCharger))
	// router.POST("/v1/api/win/call", ginplus.Wrapper(hd.GetResult))
	// router.POST("/v1/api/win/list", ginplus.Wrapper(hd.GetResultList))
	// router.POST("/v1/api/win/open", ginplus.Wrapper(hd.GetResultOpen))
	// router.POST("/v1/api/win/doll", ginplus.Wrapper(hd.GetResultDoll))
	// //获取攻略数据
	// router.POST("/v1/api/strategy", ginplus.Wrapper(hd.GetStrategy))
	// router.POST("/v2/api/strategy", ginplus.Wrapper(hd.GetStrategyH))

	// //获取图片接口
	// router.GET("/v1/api/image", ginplus.Wrapper(hd.GetImage))

	// //支持查询

	// router.POST("/v4/api/job/imitate", ginplus.Wrapper(hd.ImitateCalculatingAward))
	// router.POST("/v4/api/job/list", ginplus.Wrapper(hd.ImitateList))

	// //后台数据
	// //获取期数列表
	// router.POST("/v1/api/admin/periods", ginplus.Wrapper(hd.GetAdminPeriods))

	// //开始摇奖接口处理
	// router.POST("/v1/api/admin/start", ginplus.Wrapper(hd.StartLottery))

	// //模拟判断中奖概率接口
	// router.POST("/v1/api/admin/imitate", ginplus.Wrapper(hd.CalculateProbability))

	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

	return router, nil
}
